package team.scau.laboratory_repair.common.quartz.jobs.repair;

import lombok.extern.apachecommons.CommonsLog;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.task.Task;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import team.scau.laboratory_repair.service.JobService;
import team.scau.laboratory_repair.common.utils.spring.SpringContextHolder;
import team.scau.laboratory_repair.service.WeiXinService;

import java.util.List;

/**
 * @author 30254
 * creadtedate:2018/10/15
 */
@CommonsLog
public class CheckJob extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        JobDataMap dataMap = context.getMergedJobDataMap();
        WeiXinService weiXinService = SpringContextHolder.getBean("weiXinService");
        JobService jobService = SpringContextHolder.getBean("jobService");
        TaskService taskService = SpringContextHolder.getBean("taskService");
        String taskId = (String) dataMap.get("taskId");
        List<Task> list = taskService.createTaskQuery().taskId(taskId).list();
        if (list.size() > 0) {
            Long applicantId = (Long) list.get(0).getProcessVariables().get("applicantId");
            weiXinService.infoRepairStaff(applicantId);
            return;
        }
        try {
            jobService.removeSchedule(taskId);
        } catch (SchedulerException e) {
            log.error(e.getMessage(), e);
        }

//        log.info("check job executed");
    }
}
